package org.intership.clubmate.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import org.intership.clubmate.config.Log;
import org.intership.clubmate.entity.Club;
import org.intership.clubmate.entity.UserClubCollection;
import org.intership.clubmate.enums.HttpCode;
import org.intership.clubmate.pojo.ResponseResult;
import org.intership.clubmate.service.ClubService;
import org.intership.clubmate.service.UserClubCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins="*")
@RequestMapping("/collection")
public class UserClubCollectionController {

    @Autowired
    private UserClubCollectionService userClubCollectionService;
    @Autowired
    private ClubService clubService;

    @RequestMapping("/add")
    @Log(operaModule = "添加收藏",operaType = "ADD")
    public ResponseResult addCollection(@RequestBody UserClubCollection userClubCollection){
        UserClubCollection ucc= userClubCollectionService.add(userClubCollection);
        if(ucc==null){
            return ResponseResult.error(HttpCode.COLLECTION_NOT_NULL);
        }
        return ResponseResult.success();
    }

    @RequestMapping("/delete")
    @Log(operaModule = "取消收藏",operaType = "DELETE")
    public ResponseResult deleteCollection(@RequestBody UserClubCollection userClubCollection){
        userClubCollectionService.delete(userClubCollection.getUserId(),userClubCollection.getClubId());
        UserClubCollection ucc =userClubCollectionService.findUserClubCollection(userClubCollection.getUserId(),userClubCollection.getClubId());
        if(ucc==null){
            return ResponseResult.success();
        }
        else return ResponseResult.error(HttpCode.SYSTEM_ERROR);
    }

    //感觉没用
    @RequestMapping("/getCollection/{clubId}")
    @Log(operaModule = "获取收藏的社团",operaType = "GET")
    public ResponseResult getCollection(@RequestParam int uid,@PathVariable int clubId){
        UserClubCollection ucc =userClubCollectionService.findUserClubCollection(uid, clubId);
        if(ucc!=null){
           Club club = clubService.getById(ucc.getClubId());
           return ResponseResult.success(club);
        }else return ResponseResult.error(HttpCode.COLLECTION_NULL);
    }

    @RequestMapping("/getAll")
    @Log(operaModule = "获取收藏列表",operaType = "GET")
    public ResponseResult getALl(@RequestParam(defaultValue = "1") Integer pageNo,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam int uid){
        IPage<UserClubCollection> clubs=userClubCollectionService.getClub(pageNo,pageSize,uid);
        return ResponseResult.success(clubs);
    }


    //判断是否收藏对应社团
    @GetMapping("/getcollsta")
    @Log(operaModule = "获取收藏状态",operaType = "GET")
    public ResponseResult isCollect(@RequestParam Integer userId,
                                    @RequestParam Integer clubId){
        UserClubCollection userClubCollection=userClubCollectionService.findUserClubCollection(userId,clubId);
        if(userClubCollection!=null){
            return ResponseResult.success(true);
        }else return ResponseResult.error(HttpCode.COLLECTION_NULL);
    }

    @GetMapping("/exit")
    public ResponseResult isExit(
            @RequestParam Integer userId,
            @RequestParam Integer clubId
            ){
        UserClubCollection userClubCollection=userClubCollectionService.ifExit(userId,clubId);
        if(userClubCollection==null)
            return  ResponseResult.success(false);
        else
            return ResponseResult.success(true);
    }


}
